var app = new Vue({
	el: '#exam',
	data: {
		questions: [],
		answers: {
			id0: "",
			id1: "",
			id2: "",
			id3: "",
			id4: "",
			id5: "",
			id6: "",
			id7: "",
			id8: "",
			id9: ""
		},
		ruleAnswers: {
			id0: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id1: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id2: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id3: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id4: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id5: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id6: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id7: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id8: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}],
			id9: [{
				required: true,
				message: '还有题未作答',
				trigger: 'change'
			}]
		}
	},
	methods: {
		getPaper() {
			axios.get("/papers/questions").then(res => {
				if (res.data) {
					this.questions = res.data;
					//				    for(var i=0;i<res.data.length;i++){
					//                        this.answers["id"+res.data[i].id]=""
					//				    }
				}
			})
		},
		submitPaper() {
			var that = this;
			that.$refs.answers.validate((valid) => {
				if (valid) {
					//					var answers = ""
					//					for (var i = 0; i < that.questions.length; i++) {
					//						answers += "&answers=" + that.answers["id" + i]
					//					}
					//					axios.post("/papers/check", answers).then(res => {
					//						if (res.data && res.data == "ok") {
					//							that.$message.success('交卷成功!');
					//						}
					//					})
					var answers = []
					for (var i = 0; i < that.questions.length; i++) {
						answers.push({
							id: that.questions[i].id,
							rightAnswer: that.answers["id" + i]
						})
					}
					axios.post("/papers/check", {
						answers: answers,
						id: 1
					}, {
						headers: {
							'content-type': 'application/json'
							//'Content-Type': 'application/x-www-form-urlencoded'
						}
					}).then(res => {
						window.location.href = res.data
					})
				} else {
					that.$message.error('还有题未作答!');
				}
			})
		}
	},
	created() {
		this.getPaper();
	}
})
